package cn.com.bluemoon.common.security;

import cn.com.bluemoon.daps.common.datascope.feign.FeignHystrixConcurrencyStrategy;
import cn.com.bluemoon.daps.common.datascope.feign.FeignRequestInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

/**
 * 定义feign请求，携带源请求头信息（token传递等）
 *
 * @author Jarod Kong
 * @date 2020-12-19 01:24
 **/
@EnableFeignClients(basePackages = FeignClientAutoConfig.FEIGN_BASE_PKG, defaultConfiguration = FeignRequestInterceptor.class)
@EnableHystrix
@ConditionalOnProperty(
        value = "domp.fegin.scan",
        matchIfMissing = true
)
public class FeignClientAutoConfig {
    public static final String FEIGN_BASE_PKG = "cn.com.bluemoon";
    /**
     * 微服务之间使用Feign调用接口时, 透传header参数信息
     * 1.默认为hystrix.command.default.execution.isolation.strategy=Thread 会获取不到ServletRequestAttributes
     * 2.调整为hystrix.command.default.execution.isolation.strategy=SEMAPHORE 可以获取，但是非官方推荐
     */
    @Bean
    public FeignRequestInterceptor feignRequestInterceptor() {
        return new FeignRequestInterceptor();
    }

    /**
     * <a href="https://developer.aliyun.com/article/727560'>参考文档</a>
     */
    @Bean
    public FeignHystrixConcurrencyStrategy feignHystrixConcurrencyStrategy() {
        return new FeignHystrixConcurrencyStrategy();
    }

}
